import { Component, OnInit, ViewChild, inject } from '@angular/core';
import { FormBuilder, FormGroup } from '@angular/forms';
import { STColumn, STComponent } from '@delon/abc/st';
import { SFSchema } from '@delon/form';
import { ModalHelper, _HttpClient } from '@delon/theme';
import { SHARED_IMPORTS } from '@shared';
import { NzModalService } from 'ng-zorro-antd/modal';
import { AppValidators } from 'src/app/core/utils/app-validators';
import { BaseService } from 'src/app/core/utils/base.service';

@Component({
  selector: 'app-chrgitm-type',
  standalone: true,
  imports: [...SHARED_IMPORTS],
  templateUrl: './type.component.html'
})
export class ChrgitmTypeComponent implements OnInit {
  private readonly http = inject(_HttpClient);
  private readonly modal = inject(ModalHelper);

  editVisible = false;
  url = `/api/dc/chrgitm-type`;
  grpUrl = `/api/dc/chrgitm-type-grp`;

  chrgitmTypeGrpList: any; //收费项目类别分组
  searchSchema: SFSchema = {
    properties: {
      key: {
        type: 'string',
        title: '关键字'
      }
    }
  };
  @ViewChild('st') private readonly st!: STComponent;
  columns: STColumn[] = [
    { title: '收费类别编码', index: 'chrgitmTypeCodg' },
    { title: '收费类别名称', index: 'chrgitmTypeName' },
    { title: '分组名称', index: 'chrgitmTypeGrp.chrgitmTypeGrpName' },
    { title: '有效标识', index: 'valiFlag', type: 'yn' },
    {
      title: '',
      buttons: [
        { text: '编辑', click: (item: any) => this.edit(item) },
        { text: '删除', pop: true, click: (item: any) => this.delete(item) }
      ]
    }
  ];

  //chrgitmTypeGrp
  title = '';
  form: FormGroup = this.fb.group({
    id: [],
    chrgitmTypeCodg: ['', AppValidators.required('E0001', '收费类别编码')],
    chrgitmTypeName: ['', AppValidators.required('E0001', '收费类别名称')],
    chrgitmTypeGrpId: ['', AppValidators.required('E0001', '收费类别分组')],
    valiFlag: [false, AppValidators.required('E0001', '有效标识')]
  });

  constructor(
    private fb: FormBuilder,
    public baseService: BaseService
  ) {
    this.initChrgitmTypeGrp();
  }

  ngOnInit(): void {}

  initChrgitmTypeGrp() {
    this.http.get(this.grpUrl).subscribe(res => {
      this.chrgitmTypeGrpList = res.items;
    });
  }

  /**
   * ocean 2023-11-6
   * 添加
   */
  add(): void {
    this.title = '新增类别信息';
    this.form.reset({ valiFlag: false });
    this.editVisible = true;
  }

  /**
   * ocean 2023-11-6
   * 编辑
   */
  edit(e: { [key: string]: any }) {
    this.title = '编辑类别信息';
    this.form.patchValue(e);
    this.editVisible = true;
  }

  /**
   * ocean 2023-11-16
   * 删除
   */
  delete(e: { id: any }) {
    this.http.delete(`${this.url}/${e.id}`).subscribe(res => {
      this.st.reload();
    });
  }

  /**
   * ocean 2023-11-6
   * 保存
   */
  save() {
    // 画面输入的验证
    if (this.baseService.hasError(this.form)) {
      return;
    }
    const req = this.form.getRawValue();
    if (req.id) {
      this.http.put(`${this.url}/${req.id}`, req).subscribe(res => {
        this.editVisible = false;
        this.st.reload();
      });
    } else {
      this.http.post(this.url, req).subscribe(res => {
        this.editVisible = false;
        this.st.reload();
      });
    }
  }

  close(): void {
    this.editVisible = false;
  }
}
